﻿Option Explicit On
Option Strict On

Imports System.Threading

Public Class MainClass
    Inherits ApplicationContext

    Private WithEvents gamePad As xboxMouse.gamePadClass
    Private WithEvents monitor As xboxMouse.monitorClass
    Private WithEvents decision As xboxMouse.decisionClass

    Private siMutex As Mutex


    Public Sub New()

        AddHandler Application.ApplicationExit, AddressOf OnApplicationExit

        'Declare a mutex, if the create fails another instance of this application must be open
        siMutex = New Mutex(False, "xboxMouse")

        If (Not siMutex.WaitOne(0, False)) Then

            End

        Else
            'Create a new monitor class, this automatically starts looking for the xbox remotes
            monitor = New xboxMouse.monitorClass

            'Create new decision class.  Decised what each keypress should do
            decision = New xboxMouse.decisionClass

            'Start looking for xbox remotes with L and R pressed
            monitor.StartMonitoring()

        End If

    End Sub


    Private Sub OnApplicationExit(ByVal sender As Object, ByVal e As EventArgs)
        siMutex.ReleaseMutex()
        If Not gamePad Is Nothing Then
            gamePad.stopPolling()
            gamePad = Nothing
        End If
    End Sub


    Private Sub StartTriggered() Handles monitor.StartTriggered

        'An xbox remote was found and the shoulder buttons have been held in for 3+ second
        PlaySound(True)

    End Sub


    Private Sub triggerFinished(ByVal intPlayerIndex As Integer) Handles monitor.triggerFinished

        'The shoulder buttons have been released

        'The gamepad class handles all controller inputs and raises events
        gamePad = New xboxMouse.gamePadClass(intPlayerIndex)

        AddHandler decision.stopPolling, AddressOf gamePad.stopPolling
        AddHandler gamePad.buttonStateChanged, AddressOf decision.buttonStateChanged
        AddHandler gamePad.thumbStickMoved, AddressOf decision.ThumbStickMoved

        gamePad.startPolling()

    End Sub


    Private Sub gamepadPollingStopped() Handles gamePad.pollingStopped

        'Add event handlers
        RemoveHandler gamePad.buttonStateChanged, AddressOf decision.buttonStateChanged
        RemoveHandler gamePad.thumbStickMoved, AddressOf decision.ThumbStickMoved
        RemoveHandler decision.stopPolling, AddressOf gamePad.stopPolling

        gamePad = Nothing
        PlaySound(False)
        monitor.StartMonitoring()

    End Sub


    Private Sub PlaySound(ByVal Connected As Boolean)

        Try
            My.Computer.Audio.Play(If(Connected, "C:\Windows\Media\Speech On.wav", "C:\Windows\Media\Speech Off.wav"))
        Catch ex As Exception
        End Try

    End Sub

End Class



'#############################
'######## STARTS HERE ########
'#############################

Public Module StartModule

    Public Sub Main()

        Application.Run(New MainClass)

    End Sub

End Module